#include "globals.h"
#include <time.h>

int
main(int argc, char **argv)
{
    struct sockaddr_un servaddr, addr2;
    int portno = 4094;              /* Well-known port */
    int     sockfd_r, sockfd_w;
    char ip_addr[INET_ADDRSTRLEN];
    char msg[1000];
    int err;
    char    buff[1000];
	time_t ticks;

    int maxfd;
    fd_set rset;

    int len = sizeof(addr2);

    sockfd_r = Socket(AF_LOCAL, SOCK_DGRAM, 0);

//    sockfd_w = Socket(AF_LOCAL, SOCK_DGRAM, 0);

    printf("\n");

    FD_ZERO(&rset);

    unlink(SRV_WELL_KNOWN_PATH);

    bzero(&servaddr, sizeof(servaddr));    
    servaddr.sun_family = AF_LOCAL;
    strcpy(servaddr.sun_path, SRV_WELL_KNOWN_PATH);

    err = bind(sockfd_r, (SA *) &servaddr, sizeof(servaddr));
    check_err(err, "bind sockdgram");

    Getsockname(sockfd_r, &addr2, &len);

    printf("[%s]\n", addr2.sun_path);
    printf("Go for server.\n");
	
    while(1)
    {
    	printf("Waiting for Client Request ... \n");
	    msg_receive(sockfd_r, msg, ip_addr, &portno);
		
		ticks = time(NULL);
		snprintf(buff, sizeof(buff), "%.24s", ctime(&ticks));
		printf("The buffer: %s\n", buff);
        printf("Sending time stamp back to the client: %s:%d.\n", ip_addr, portno);
        msg_send(sockfd_r, ip_addr, portno, buff, 0); 
    }

    return 0;
}


